<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <script src="/static/js/jquery/jquery.js" type="text/javascript"></script>
    <script src="/static/js/jquery/jquery.cookie.js" type="text/javascript"></script>
    <style>
        #msg{
            padding:10px;
        }
        #msg div
        {
            margin: 5px;
            padding: 5px 20px;
            border:1px solid #e2e2e2;
        }
    </style>
</head>
<body>
<div>短信测试接口，请用谷歌浏览器，请允许通知接口</div>
<div style="color: red;" id="msg">

</div>
<script>
    {literal}
    $(function(){
        var sec = 5;
        //console.log(Notification.close);
        //var pre = $.cookie("_SMS_PRE_");
        var notification;
        function fix(n)
        {
            n = n.toString();
            return n.length>1?n:0+n;
        }
        function notify($title,$body)
        {
            var msgBox = $("#msg");
            var date = new Date();
            msgBox.prepend("<div><h3>"+$title+"<span style='padding-left: 10px;'>"+fix(date.getHours())+":"+fix(date.getMinutes())+":"+fix(date.getSeconds())+"</span></h3><p>"+$body+"</p></div>");
            if (!("Notification" in window)) {
                return;
            }
            Notification.requestPermission(function(result) {
                if (result === 'denied') {
                    console.log('Permission wasn\'t granted. Allow a retry.');
                    return;
                } else if (result === 'default') {
                    console.log('The permission request was dismissed.');
                    return;
                }
                do_notify($title,$body);
            });
        }
        function do_notify($title,$body){
            if(notification)
            {
                notification.close();
            }
            notification = new Notification($title,{
                body:$body||""
            });
        }
        function get_sms()
        {
            /*$.getJSON("/test/sms_notify",function(ret){
                if(ret.success)
                {
                    notify(ret.mobile,ret.msg);
                    //console.log(data);
                    //notify(data.mobile,data.msg);
                }
                setTimeout(get_sms,3000);
            });*/
            $.ajax({
                type:"get",
                dataType:"json",
                url:"/test/sms_notify",
                success:function(ret){
                    if(ret.success)
                    {
                        notify(ret.mobile,ret.msg);
                    }
                    setTimeout(get_sms,3000);
                },
                error:function(){
                    setTimeout(get_sms,3000);
                }
            });
            //setTimeout(get_sms(),5000);
        }
        get_sms();
        //notify("测试");
    });
    {/literal}
</script>
</body>
</html>